/*
 * @Author: ChaiHongJun
 * @Date: 2021-07-10 09:30:50
 * @LastEditors: ChaiHongJun
 * @LastEditTime: 2021-07-15 17:17:54
 * @version:
 * @Description:
 */
/**
 * 本地存储封装模块
 *  localStorage 只能存储字符串数据
 *  如果要存储Json
 *  Json 可以转化成 字符串再存储
 * 
 *  取出的时候再将字符串转化为字符串
 */
/**
 * @name: 数据存储到 localStorage
 * @param {name} object|String
 * @return {*}
 */
export const setItem = (name, value) => {
  // 如果 value 是对象，就把 value 转为 JSON 格式字符串再存储
  if (typeof value === "object") {
    value = JSON.stringify(value);
  }
  window.localStorage.setItem(name, value);
}
/**
 * @name:从localStorage 读取数据
 * @param {name}
 * @return {*}
 */
export const getItem = (name) => {
  const data = window.localStorage.getItem(name)
  try {
    return JSON.parse(data);
  } catch (err) {
    return data;
  }
}
export const removeItem = (name) => {
  window.localStorage.removeItem(name);
}